<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>对象的学习</title>
</head>
<body>
    <script>
        //使用{}创建对象
        var student = {
            name: "蔡徐坤",
            height: 177,
            sayHello: function () {
                console.log("你好");
            }  
        }
        //访问对象的属性和方法
        //1.使用.访问对象的属性
        console.log(student.name);//蔡徐坤
        //2.使用[]访问对象的属性
        console.log(student["name"]);//蔡徐坤
        //3.使用.访问对象的方法
        console.log(student.height);//177
        student.sayHello();//你好
        //修改对象的属性和方法
        student.name = "蔡徐坤2";
        console.log(student.name);//蔡徐坤2
        //添加对象的属性和方法
        student.age = 18;
        console.log(student.age);//18

        //使用new Object()创建对象
        var student2 = new Object();
        student2.name = "蔡徐坤3";
        student2.height = 177;
        student2["weight"] = 60;
        student2.sayHello = function () {
            console.log("你好");
        }
        console.log(student2.name);//蔡徐坤3
        console.log(student2.height);//177
        console.log(student2["weight"]);//60
        student2.sayHello();//你好

        //使用构造函数创建对象
        function Student(name, height) {
            this.name = name;
            this.height = height;
            this.sayHello = function () {
                console.log("你好");
            }
        }
        //使用new关键字创建对象，必须使用new关键字
        var student3 = new Student("蔡徐坤4", 177);
        console.log(student3.name);//蔡徐坤4
        console.log(student3.height);//177
    </script>
</body>
</html>